﻿--[[
   @brief    client global tips show frame
   @author   jadonsun
   @version  1.0
   @date     2017-04-18
--]]
client_tips_control_constant = {
    click_url_type_win = 1;
    click_url_type_img = 2;
    click_url_type_main = 3;
    click_url_type_sub = 4;
    click_url_type_third = 5;
    click_ok_btn = 6;
    click_cancel_btn = 7;
    click_set_btn = 8;

    close_type_timeout = 9;
    close_type_close_btn = 10;

    SHOW_TIPS_INTERVAL = 200;
    CLOSE_TIPS_INTERVAL = 50;

    OSS_TIME_OUT_NUM = 15;  -- 间隔为3s左右
}

app_exit_tips_actions = {
    kAppExitTipsShow = "show",
    kAppExitTipsCloseActive = "close_active",
    kAppExitTipsClosePassive = "close_passive",
    kAppExitTipsClick = "click",
    kAppExitTipsCloseOneWeek = "close_oneweek"
};

client_tips_frame_control =
{
    is_bubble_moving = false; -- 用于标记气泡tips是否在移动中

    is_show_animation = false;

    is_normal_moving_by_dragging = false;  -- 记录当前移动是否通过拖动完成

    -- 以下两个参数用户更严格限制tips显示时间，防止“tips正在显示或正在消失时，再消失或显示tips位置不对问题”
    is_closing_tip = false;  -- 标记是否正在close tips
    is_showing_tip = false;  -- 标记是否正在显示tips

    click_tip_data = nil;
    click_url_type = 0;

    oss_query_count = 0;

    -- 计数器，每出现一个重复tip_id的tip时，则增加一次，存储到tip_data中，从而保证tip_id的唯一性
    repeat_tip_count = 0;

    tips_click_map = {};
        
    on_login_success = function()
        base_utility.log("[client_tips_frame_control] load");
    end;

    init = function()
        base_utility.log("[client_tips_frame_control] init");
        ierd_tgp.tpf_ui.Require("client_tips_frame\\view.lua");
        ierd_tgp.tpf_ui.Require("client_tips_frame\\model.lua");
        client_tips_frame_model.init();
        client_tips_frame_view.init();
        client_tips_frame_control.init_tips_click_map();
    end;

    init_tips_click_map = function()
        client_tips_frame_control.tips_click_map = {
            ok = client_tips_frame_control.click_ok_normal_tips;
            cancel = client_tips_frame_control.click_cancel_normal_tips;
            close = client_tips_frame_control.click_close_normal_tips;
            setting = client_tips_frame_control.click_setting_normal_tips;

            main_title = client_tips_frame_control.click_main_title;
            sub_title = client_tips_frame_control.click_sub_title;
            third_title = client_tips_frame_control.click_third_title;
            img = client_tips_frame_control.click_img_title;
        };
    end;

    -- 收到显示Tips请求
    receive_tips = function (tips_data)
        if base_utility.is_big_picture_ui() then
            return;
        end

        if(tips_data ~= nil) then
            local tips_main_type = tips_data.tips_main_type;

            local wait_tips_num = #(client_tips_frame_model.all_tips_queue);
            tips_data.id = tonumber(tips_data.id);
            if tips_data.id == 0 then
                tips_data.id = os.time();
            end
            base_utility.log("[client_tips_frame_control] receive a tip, tip_id = ".. tips_data.id);
            base_utility.log("[client_tips_frame_control] tips data:" ..
                    base_utility.table_to_json(tips_data));
            client_tips_frame_model.all_tips_queue[wait_tips_num + 1] = tips_data;
            if wait_tips_num == 0 then
                client_tips_frame_control.start_timer();
            end;
        end;

    end;

    show_tips = function (tips_data)
        local opt = base_utility.copy_table_without_function(tips_data);
        tips_data.native = true;
        base_utility.log("[client_tips_control]show_tips, " .. base_utility.table_to_json(opt));

        -- 删除测试代码
        -- tips_data.tips_main_type = client_tips_model_constant.TIPS_MAIN_TYPE_BUBBLE;
        -- tips_data.tips_type_bubble = 2;
        -- tips_data.show_duration = 1000000;
        -- opt = base_utility.copy_table_without_function(tips_data);
        -- base_utility.log("[client_tips_control]test show_tips, " .. base_utility.table_to_json(opt));
        -- 测试代码
        local tips_main_type = tonumber(tips_data.tips_main_type);
        if(tips_main_type == client_tips_model_constant.TIPS_MAIN_TYPE_NORMAL) then
            -- 通用tips
            client_tips_frame_control.show_normal_tips(tips_data);
        elseif (tips_main_type == client_tips_model_constant.TIPS_MAIN_TYPE_BUBBLE) then
            -- 气泡tips
            if tips_data.show_duration == nil or tips_data.show_duration == 0 then
                -- 设置bubble tip默认消失时间
                tips_data.show_duration = client_tips_model_constant.TIPS_BUBBLE_TIMEOUT;
            end;
            client_tips_frame_control.show_bubble_tips(tips_data);
        elseif (tips_main_type == client_tips_model_constant.TIPS_MAIN_TYPE_WEB) then
            -- web tips
            client_tips_frame_control.show_web_tips(tips_data);
        else
            base_utility.log("[client_tips_control][show_tips]unknown main_type, " ..
                    base_utility.table_to_json(opt));
        end;
    end;

    -- 异步定时器
    start_timer = function()
        local tpfui = ierd_tgp.tpf_ui.GetTPFUI();
        if nil ~= tpfui then
            tpfui:AddInterval("client_tips_frame_control.on_timer",
                    client_tips_control_constant.SHOW_TIPS_INTERVAL, 0);
        end;
    end;

    -- 停止定时器
    stop_timer = function()
        local tpfui = ierd_tgp.tpf_ui.GetTPFUI();
        if nil ~= tpfui then
            tpfui:RemoveInterval("client_tips_frame_control.on_timer", 0);
        end;
    end;

    -- 异步事件处理
    on_timer = function()
        local wait_tips_num = #(client_tips_frame_model.all_tips_queue);
        if wait_tips_num > 0 then
            local is_oss_req_finish = client_tips_frame_control.is_oss_req_finish();
            -- 如果请求oss还没结束，则继续下次循环
            if is_oss_req_finish == false then
                client_tips_frame_control.oss_query_count = client_tips_frame_control.oss_query_count + 1;
                base_utility.log("[client_tips_frame_control]wait for oss return, wait time = ".. client_tips_frame_control.oss_query_count);
                return;
            end;
            if client_tips_frame_control.is_closing_tip == true then
                -- 正在关闭tips，下个计时器时间再显示
                base_utility.log("[client_tips_frame_control] is closing tip, not show ,return");
                return;
            end;
            if client_tips_frame_control.is_show_animation == true then
                -- 正在播放动画，下个计时器再显示
                base_utility.log("[client_tips_frame_control] is show animation, not show ,return");
                return;
            else

                local tips_data_temp = client_tips_frame_model.all_tips_queue[1];
                -- 判断是否是已经注册过的
                local tips_sub_type = tonumber(tips_data_temp.tips_sub_type);
                -- 加入tips子类型判断，保证是正常接入的tips
                if (client_tips_frame_control.is_tips_register(tips_sub_type) == false) then
                    -- 没有注册的tips，不予以显示
                    base_utility.log("[client_tips_frame_control]this tip type is not register, not show ,tip_id="..tips_data_temp.id);
                    table.remove(client_tips_frame_model.all_tips_queue, 1);
                    return;
                end;
                table.remove(client_tips_frame_model.all_tips_queue, 1);
                client_tips_frame_control.show_tips(tips_data_temp);
            end;
        else
            client_tips_frame_control.stop_timer();
        end;
    end;

    -- 通用tips
    show_normal_tips = function (tips_data)
        local tips_num = #(client_tips_frame_model.normal_tips_queue);
        -- 判断是否tip_id相同
        for i = 1, tips_num do
            if client_tips_frame_model.normal_tips_queue[i].id == tips_data.id then
                client_tips_frame_control.repeat_tip_count = client_tips_frame_control.repeat_tip_count + 1;
                tips_data.repeat_num = client_tips_frame_control.repeat_tip_count;
                tips_data.id = tips_data.id + tips_data.repeat_num;
                break;
            end;
        end;
        table.insert(client_tips_frame_model.normal_tips_queue, tips_num + 1, tips_data);
        client_tips_frame_view.show_normal_tips(tips_data);
    end;

    -- 气泡tips
    show_bubble_tips = function (tips_data)
        -- 判断当前有几个tips存在，如果大于TOTAL_BUBBLE_TIPS_NUM,则丢弃，否则显示
        local tips_num = #(client_tips_frame_model.bubble_tips_queue);
        if(tips_num >= client_tips_model_constant.TOTAL_BUBBLE_TIPS_NUM) then
            -- 当前已有多于TOTAL_BUBBLE_TIPS_NUM个tips在显示,移除old的一个
            local tips_to_remove_id = client_tips_frame_model.bubble_tips_queue[1].id;
            -- 查找tips dlg id
            local dlg_to_remove_id = client_tips_frame_model.get_bubble_tip_dlg_id(tips_to_remove_id);
            client_tips_frame_view.close_bubble_tips_dlg(tips_to_remove_id, client_tips_control_constant.close_type_timeout);
            base_utility.log("[client_tips_frame_control] A tip is closing, put back to wait queue, tip_id="..tips_data.id);
            client_tips_frame_control.receive_tips(tips_data);
        else
            client_tips_frame_control.is_showing_tip = true;
            tips_num = #(client_tips_frame_model.bubble_tips_queue);
            -- 判断是否tip_id相同
            for i = 1, tips_num do
                if client_tips_frame_model.bubble_tips_queue[i].id == tips_data.id then
                    client_tips_frame_control.repeat_tip_count = client_tips_frame_control.repeat_tip_count + 1;
                    tips_data.repeat_num = client_tips_frame_control.repeat_tip_count;
                    tips_data.id = tips_data.id + tips_data.repeat_num;
                    break;
                end;
            end;
            table.insert(client_tips_frame_model.bubble_tips_queue, tips_num + 1, tips_data);
            base_utility.log("[client_tips_frame_control] No tip is closing, will show the tip, tip_id="..tips_data.id);
            client_tips_frame_view.show_bubble_tips(tips_data);
        end;
    end;

    -- web tips
    show_web_tips = function (tips_data)
        -- body
        base_utility.log("[client_tips_frame_control]show_web_tips， tips_data:" ..
                base_utility.table_to_json(tips_data));

        local tpf_ui =
        base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");

        local sys_param = ierd_tgp.tpf_ui.SysParams:new();
        tpf_ui:get_sys_params(sys_param);
        cur_resolution_x = sys_param.screen_width;
        cur_resolution_y = sys_param.screen_height;
        cur_work_area_width = sys_param.work_area_width;
        cur_work_area_height = sys_param.work_area_height;
        sys_param:delete();

        tips_data.screen_width = cur_work_area_width;
        tips_data.screen_height = cur_work_area_height;
        tips_data.version = 2;

        event_center.send_event(CommonTips_event.SHOW_COMMON_WEB_TIPS,
                base_utility.table_to_json(tips_data));
    end;

    -- 关闭通用tips
    click_close_bubble_tips = function (uilib, control_id)
        -- 获得dlg
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        local tip_id = client_tips_frame_model.get_bubble_tip_id(dlg_id);
        client_tips_frame_view.close_bubble_tips_dlg(tip_id,
                client_tips_control_constant.close_type_close_btn);
    end;

    -- 关闭通用tips
    click_close_normal_tips = function (uilib, control_id)
        base_utility.log("[client_tips_frame_control]click_close_normal_tips " ..
                tostring(control_id));
        -- 获得dlg
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        local tip_id = client_tips_frame_model.get_normal_tip_id(dlg_id);
        client_tips_frame_view.close_normal_tips_dlg(tip_id,
                client_tips_control_constant.close_type_close_btn);
    end;

    -- 点击确定按钮
    click_ok_normal_tips = function (uilib, control_id)
        base_utility.log("[client_tips_frame_control]click_ok_normal_tips " ..
                tostring(control_id));
        -- 获得dlg
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        base_utility.log("[client_tips_frame_control]click_ok_normal_tips_impl" ..
                tostring(dlg_id));
        if not dlg_id then
            base_utility.log("[client_tips_frame_control][click_ok_normal_tips_impl]dlg_id is nil",
                    true);
        end
        local tip_id = client_tips_frame_model.get_normal_tip_id(dlg_id);
        -- 获得待返回的数据
        local ret_data = client_tips_frame_control.get_return_data(tip_id, client_tips_control_constant.click_ok_btn);
        client_tips_frame_view.close_normal_tips_dlg(tip_id,
                    client_tips_control_constant.click_ok_btn);
        if ret_data ~= nil and ret_data.ok_service ~= nil then
            local ok_service_data = ret_data.ok_service_data or ret_data;
            client_tips_frame_control.send_tips_broadcast(ret_data.ok_service, ok_service_data);
        end;
    end;

    -- 点击取消按钮
    click_cancel_normal_tips = function (uilib, control_id)
        -- 获得dlg
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        local tip_id = client_tips_frame_model.get_normal_tip_id(dlg_id);
        -- 获得待返回的数据
        local ret_data = client_tips_frame_control.get_return_data(tip_id, client_tips_control_constant.click_cancel_btn);
        client_tips_frame_view.close_normal_tips_dlg(tip_id,
                   client_tips_control_constant.click_cancel_btn);
        if ret_data ~= nil and ret_data.cancel_service ~= nil then
            local cancel_service_data = ret_data.cancel_service_data or ret_data;
            client_tips_frame_control.send_tips_broadcast(ret_data.cancel_service, cancel_service_data);
        end;
    end;

    -- 点击设置按钮
    click_setting_normal_tips = function (uilib, control_id)
        -- 获得dlg
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        local tip_id = client_tips_frame_model.get_normal_tip_id(dlg_id);
        -- 获得待返回的数据
        local ret_data = client_tips_frame_control.get_return_data(tip_id, client_tips_control_constant.click_set_btn);
        if ret_data ~= nil and ret_data.setting_service ~= nil then
            ret_data.close_type = client_tips_control_constant.click_set_btn;
            local setting_service_data = ret_data.setting_service_data or ret_data;
            client_tips_frame_control.send_tips_broadcast(ret_data.setting_service, setting_service_data);
        end;
    end;

    click_close_bkd_tips = function (uilib, control_id)
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        client_tips_frame_view.close_bkd_tips_dlg(dlg_id);
    end;

    click_bkd_tips_background = function (uilib, control_id)
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        client_tips_frame_view.click_bkd_tips_background(dlg_id);
    end;

    on_remind_ctrl_checked = function(uilib, ctrl_id)
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(ctrl_id);
        local cfg = client_tips_frame_model.get_app_exit_config_by_dlg(dlg_id);
        if nil ~= cfg and nil ~= cfg.game_id then
            client_tips_frame_model.set_app_exit_local_config(cfg.game_id);
            client_tips_frame_control.report_bkd_tips_operation(cfg.game_id
                                , app_exit_tips_actions.kAppExitTipsCloseOneWeek);
        end
    end;

    on_remind_ctrl_uncheck = function(uilib, ctrl_id)
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(ctrl_id);
        local cfg = client_tips_frame_model.get_app_exit_config_by_dlg(dlg_id);
        if nil ~= cfg and nil ~= cfg.game_id then
            client_tips_frame_model.set_app_exit_local_config(cfg.game_id, 0);
        end
    end;

    get_return_data = function (tip_id, close_type)
        local tip_data = client_tips_frame_model.get_normal_tip_by_id(tip_id);
        local ret_data = {};
        if tip_data.repeat_num ~= nil then
            -- 增加repeat_num后的id,还原出真实的id
            ret_data.id = tip_data.id - tip_data.repeat_num;
        else
            ret_data.id = tip_data.id;
        end;
        
        ret_data.tips_main_type = tip_data.tips_main_type;
        ret_data.tips_sub_type = tip_data.tips_sub_type;
        if tip_data.tips_plugin_id ~= nil then
                ret_data.tips_plugin_id = tip_data.tips_plugin_id;
        end;
        if tip_data.ok_service ~= nil then
            ret_data.ok_service = tip_data.ok_service;
        end;
        if tip_data.cancel_service ~= nil then
            ret_data.cancel_service = tip_data.cancel_service;
        end;
        if tip_data.setting_service ~= nil then
            ret_data.setting_service = tip_data.setting_service;
        end;
        tip_data.close_type = close_type;
        return tip_data;
    end;

    -- 点击主标题的响应
    click_main_title = function (uilib, control_id)
        base_utility.log("[client_tips_frame_control]click_main_title " ..
                tostring(control_id));
        client_tips_frame_control.response_click(control_id,
                    client_tips_control_constant.click_url_type_main);
    end;

    -- 点击主标题的响应
    click_main_title_btn = function (uilib, control_id)
        client_tips_frame_control.response_btn_child_click(client_tips_control_constant.click_url_type_main, control_id);
    end;

    -- 点击副标题的响应
    click_sub_title = function (uilib, control_id)
        client_tips_frame_control.response_click(control_id,
                    client_tips_control_constant.click_url_type_sub);
    end;

    -- 点击主标题的响应
    click_sub_title_btn = function (uilib, control_id)
        client_tips_frame_control.response_btn_child_click(client_tips_control_constant.click_url_type_sub, control_id);
    end;

    -- 点击第三标题的响应
    click_third_title = function (uilib, control_id)
        client_tips_frame_control.response_click(control_id,
                    client_tips_control_constant.click_url_type_third);
    end;

    -- 点击主标题的响应
    click_third_title_btn = function (uilib, control_id)
        client_tips_frame_control.response_btn_child_click(client_tips_control_constant.click_url_type_third, control_id);
    end;

    -- 点击图片时的响应函数
    click_img_title = function (uilib, control_id)
        client_tips_frame_control.response_click(control_id,
                    client_tips_control_constant.click_url_type_img);
    end;

    -- 窗口位置发生改变事件
    on_normal_tips_pos_change = function (uilib, control_id)
        if client_tips_frame_control.is_normal_moving_by_dragging == false then
            return;
        end;
        local move_tip_id = client_tips_frame_model.get_normal_tip_id(control_id);
        local move_tip = client_tips_frame_model.get_normal_tip_by_id(move_tip_id);
        local move_tip_normal_type = 0;
        if move_tip ~= nil and move_tip.tips_type_normal ~= nil and move_tip.tips_type_normal == 1 then
            move_tip_normal_type = 1;
        end;
        local need_move, move_height = client_tips_frame_view.need_move_bubble_for_move(move_tip_normal_type);
        local move_width = 0;
        if need_move == true and client_tips_frame_control.is_bubble_moving == false then
            -- 需要tips
            if move_height == 0 then
                move_width, move_height = ui_utility.get_control_size_by_id(control_id);
            end;
            local move_tip_id = client_tips_frame_model.get_normal_tip_id(control_id);
            if move_tip_id ~= 0 then
                move_height = move_height - client_tips_view_constant.TIPS_SHADOW_WIDTH;
                client_tips_frame_view.move_bubble_tips_pos(false, move_height);
                client_tips_frame_control.is_bubble_moving = true;
            else
                base_utility.log("[on_normal_tips_pos_change] pos changed, is close dlg");
            end;
            
        end;
    end;

    -- 记录左键按下
    on_lbutton_down = function (uilib, control_id)
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        --fixme bring tips window to front when user click
        ui_utility.setproperty_by_id(dlg_id, "AlwaysOnTop", "true");
        ui_utility.setproperty_by_id(dlg_id, "AlwaysOnTop", "false");
        local tip_type = client_tips_frame_model.get_dlg_type(dlg_id);
        if tip_type ~= client_tips_model_constant.TIPS_MAIN_TYPE_BUBBLE then
            client_tips_frame_control.is_bubble_moving = false;
        end;
        if tip_type == client_tips_model_constant.TIPS_MAIN_TYPE_NORMAL then
            client_tips_frame_control.is_normal_moving_by_dragging = true;
        end;
    end;

    -- 记录左键弹起
    on_lbutton_up = function (uilib, control_id)
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        local tip_type = client_tips_frame_model.get_dlg_type(dlg_id);
        if tip_type ~= client_tips_model_constant.TIPS_MAIN_TYPE_BUBBLE then
            client_tips_frame_control.is_bubble_moving = true;
            return;
        end;

        if tip_type == client_tips_model_constant.TIPS_MAIN_TYPE_NORMAL then
            client_tips_frame_control.is_normal_moving_by_dragging = false;
        end;

    end;

    on_wind_click = function (uilib, control_id)
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        local tip_id = client_tips_frame_model.get_bubble_tip_id(dlg_id);
        local tip_data = client_tips_frame_model.get_bubble_tip_by_id(tip_id);
        if tip_data == nil then
            base_utility.log("[client_tips_frame_control]click windown, tip_data is nil");
            return;
        end;

        if tip_data.icon_click_url ~= nil or 
            tip_data.main_title_url ~= nil or 
            tip_data.sub_title_url ~= nil or
            tip_data.third_title_url ~= nil then
            -- 配置了其他跳转链接，不响应窗口点击
            base_utility.log("[client_tips_frame_control]has other url ,not response win click");
            return;
        end;
        client_tips_frame_control.open_url(tip_data,
                    client_tips_control_constant.click_url_type_win);
    end;

    -- 关闭动画
    on_animation_start = function (uilib, control_id)
        client_tips_frame_control.is_show_animation = true;
    end;

    on_animation_end = function (uilib, control_id)
        client_tips_frame_control.is_show_animation = false;
        client_tips_frame_control.is_closing_tip = false;
        client_tips_frame_control.is_showing_tip = false;
    end;

    on_bubble_dlg_mouse_hover = function (uilib, control_id)
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        ui_utility.setproperty_by_path(dlg_id,
                "title_close_btn", "Visible",
                "true");
    end;

    on_bubble_dlg_mouse_leave = function (uilib, control_id)
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        ui_utility.setproperty_by_path(dlg_id,
                "title_close_btn", "Visible",
                "false");
    end;

    -- 根据control_id搜索得到窗口id和窗口ptr
    get_dlg_ctrl_info = function (control_id)
        local parent_id = ui_utility.get_ctrl_parent_id(control_id);
        while(parent_id ~= Tenio.ROOT_CONTROLID)
        do
            control_id = parent_id;
            parent_id = ui_utility.get_ctrl_parent_id(control_id);
        end;
        local dlg_id = control_id;
        local dlg_ptr = nil;
        if(dlg_id > 0 and dlg_id ~= Tenio.ROOT_CONTROLID) then
            dlg_ptr = ui_utility.get_ctrl_ptr_by_id(dlg_id);
        end;
        return dlg_id, dlg_ptr;
    end;

    -- 判断当前tips是否在申请列表中
    is_tips_register = function (tips_id)
        local reg_tips_num = #(client_tips_frame_model.tips_id_list);
        if reg_tips_num == 0 then
            -- 没有拉取到OSS数据时，认为存在，可以显示tips
            return true;
        end;
        local is_register = false;
        for index = 1, reg_tips_num do
            if client_tips_frame_model.tips_id_list[index] == tips_id then
                is_register = true;
                break;
            end;
        end;
        return is_register;
    end;

    is_oss_req_finish = function ()
        local reg_tips_num = #(client_tips_frame_model.tips_id_list);
        if reg_tips_num == 0 and client_tips_frame_control.oss_query_count < client_tips_control_constant.OSS_TIME_OUT_NUM then
            -- 没有拉取到OSS数据时，且等到oss时间未到
            return false;
        else
            return true;
        end;
    end;

    -- 发送tips点击后的广播接口:broadcast_name:广播名称;broadcast_data:广播数据
    send_tips_broadcast = function (broadcast_name, broadcast_data)
        if nil ~= broadcast_name then
            local broadcast_name_len = string.len(broadcast_name);
            if broadcast_name_len > 0 then
                -- table to json
                -- local json_to_send = base_utility.table_to_json(broadcast_data);
                comm_center.send_broadcast(broadcast_name, broadcast_data);
            end;
        end;
    end;

    -- 响应对tips中的某个位置的点击
    response_click = function (control_id, click_type)

        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        local tip_type = client_tips_frame_model.get_dlg_type(dlg_id);
        local tip_data = nil;
        if tip_type == client_tips_model_constant.TIPS_MAIN_TYPE_NORMAL then
            local tip_id = client_tips_frame_model.get_normal_tip_id(dlg_id);
            tip_data = client_tips_frame_model.get_normal_tip_by_id(tip_id);
        elseif tip_type == client_tips_model_constant.TIPS_MAIN_TYPE_BUBBLE then
            local tip_id = client_tips_frame_model.get_bubble_tip_id(dlg_id);
            tip_data = client_tips_frame_model.get_bubble_tip_by_id(tip_id);
        end;

        if tip_data == nil then
            base_utility.log("[client_tips_frame_control]click tips, tip_data is nil");
            return;
        end;

        client_tips_frame_control.open_url(tip_data, click_type);
    end;

    -- 响应对tips中的某个位置下的btn点击
    response_btn_child_click = function (click_type, control_id)
        local dlg_id, dlg_ptr = client_tips_frame_control.get_dlg_ctrl_info(control_id);
        local tip_type = client_tips_frame_model.get_dlg_type(dlg_id);
        local tip_data = nil;
        if tip_type == client_tips_model_constant.TIPS_MAIN_TYPE_NORMAL then
            local tip_id = client_tips_frame_model.get_normal_tip_id(dlg_id);
            tip_data = client_tips_frame_model.get_normal_tip_by_id(tip_id);
        elseif tip_type == client_tips_model_constant.TIPS_MAIN_TYPE_BUBBLE then
            local tip_id = client_tips_frame_model.get_bubble_tip_id(dlg_id);
            tip_data = client_tips_frame_model.get_bubble_tip_by_id(tip_id);
        end;

        if tip_data == nil then
            base_utility.log("[client_tips_frame_control]click tips, tip_data is nil");
            return;
        end;
        -- 判断是否要响应点击
        if click_type == client_tips_control_constant.click_url_type_main then
            if tip_data.main_title_url == nil then
                return;
            end;
        elseif click_type == client_tips_control_constant.click_url_type_sub then
            if tip_data.sub_title_url == nil then
                return;
            end;
        elseif click_type == client_tips_control_constant.click_url_type_third then
            if tip_data.third_title_url == nil then
                return;
            end;
        end;
        
        client_tips_frame_control.open_url(tip_data, click_type);
    end;

    -- 点击后响应打开对应的url
    open_url = function (click_tip_data, click_type)
        if wegame_mainframe_view == nil then
            base_utility.log("[client_tips_frame_control] main frame is nil");
            return;
        end;
        local url = client_tips_frame_control.get_jump_url(click_tip_data, click_type);
        if url == nil or url == "" then
            base_utility.log("[client_tips_frame_control]get jump url is nil, url type="..click_type);
            return;
        end;
        base_utility.log("[client_tips_frame_control] tgp is open status");
        comm_center.call_service("i_service_open_link", url);
        -- 关闭窗口
        client_tips_frame_control.close_tip_open_url(click_tip_data, click_type);
        -- 清空记录的点击url数据
        client_tips_frame_control.click_tip_data = nil;
        client_tips_frame_control.click_url_type = 0;
    end;


    -- 根据点击类型，查找跳转连接
    get_jump_url = function (click_tip_data, url_type)
        -- 如果当前没有配置各个子部位的点击，则响应窗口的点击,并设置类型为点击窗口类型
        if url_type == client_tips_control_constant.click_url_type_win then
            return click_tip_data.win_click_url;
        elseif url_type == client_tips_control_constant.click_url_type_img then
            if click_tip_data.icon_click_url ~= nil 
                    and click_tip_data.icon_click_url ~= "" then
                return click_tip_data.icon_click_url;
            else
                client_tips_frame_control.click_url_type = client_tips_control_constant.click_url_type_win;
                return click_tip_data.win_click_url;
            end;
            
        elseif url_type == client_tips_control_constant.click_url_type_main then
            if click_tip_data.main_title_url ~= nil 
                    and click_tip_data.main_title_url ~= "" then
                return click_tip_data.main_title_url;
            else
                client_tips_frame_control.click_url_type = client_tips_control_constant.click_url_type_win;
                return click_tip_data.win_click_url;
            end;
        elseif url_type == client_tips_control_constant.click_url_type_sub then
            if click_tip_data.sub_title_url ~= nil 
                    and click_tip_data.sub_title_url ~= "" then
                return click_tip_data.sub_title_url;
            else
                client_tips_frame_control.click_url_type = client_tips_control_constant.click_url_type_win;
                return click_tip_data.win_click_url;
            end;
        elseif url_type == client_tips_control_constant.click_url_type_third then
            if click_tip_data.third_title_url ~= nil 
                    and click_tip_data.third_title_url ~= "" then
                return click_tip_data.third_title_url;
            else
                client_tips_frame_control.click_url_type = client_tips_control_constant.click_url_type_win;
                return click_tip_data.win_click_url;
            end;
        end;
    end;

    -- 点击tips时，关闭窗口
    close_tip_open_url = function(tip_data, click_type)
        if tip_data == nil then
            base_utility.log("[client_tips_frame_control] close tip when open url,tip_data is nil");
            return;
        end;
        local dlg_id = client_tips_frame_model.get_normal_tip_dlg_id(tip_data.id);
        if dlg_id > 0 then
            -- 点击的是通用tips
            client_tips_frame_view.close_normal_tips_dlg(tip_data.id, click_type);
            return;
        end;
        dlg_id = client_tips_frame_model.get_bubble_tip_dlg_id(tip_data.id);
        if dlg_id > 0 then
            -- 点击的是bubble tips
            client_tips_frame_view.close_bubble_tips_dlg(tip_data.id, click_type);
            return;
        end;
        -- 后续可以加上对web tips的处理
    end;

    report_bkd_tips_operation = function(game_id, action)
        local cfg = client_tips_frame_model.get_app_exit_config(tonumber(game_id));
        if nil == cfg then
            return;
        end
        local qos_data = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local kv_data = {};
        kv_data["update_time"] = cfg.item_update_time;
        kv_data["game_id"] = cfg.game_id or "";
        kv_data["action" ] = action;
        qos_data.oc_type    = 1;
        qos_data.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kGameUpdateLackSpace;
        qos_data.str_action = "wegame_app";
        qos_data.str_type   = "popup";
        qos_data.str_json   = base_utility.table_to_json(kv_data);
        base_utility.lua_qos_ex(qos_data);
        qos_data:delete();
    end;

    on_svr_tips_clicked = function(data)
        base_utility.log("[client_tips_frame_control]on_svr_tips_ok_clicked" ..
                base_utility.table_to_json(data));
        local dlg_id = data.win_id;
        local item = data.item;

        local web_id = ui_utility.get_ctrl_id_by_path(dlg_id, "web_container");
        if web_id <= 0 then
            base_utility.log("[client_tips_frame_control][on_svr_tips_ok_clicked]web NOT Found" ..
                    base_utility.table_to_json(data), true);
            return;
        end

        local handle = client_tips_frame_control.tips_click_map[item];
        if not handle then
            base_utility.log("[client_tips_frame_control][on_svr_tips_ok_clicked]" ..
                    base_utility.table_to_json(data), true);
            return;
        end

        base_utility.log("[client_tips_frame_control][on_svr_tips_ok_clicked]call handle for " .. tostring(item));
        handle(web_id, web_id);
    end;
}

client_tips_frame_control.init();